import java.util.ArrayList;
import java.util.List;

/**
 * 1903. 字符串中的最大奇数
 * https://leetcode-cn.com/problems/largest-odd-number-in-string/
 */
public class Solutions_1903 {
    public static void main(String[] args) {
        String num1 = "52";  // output: "5"
        String num2 = "4206";  // output: ""
        String num3 = "35427";  // output: "35427"

        List<String> inputs = new ArrayList<>();
        inputs.add(num1);
        inputs.add(num2);
        inputs.add(num3);

        for (String input : inputs) {
            String result = largestOddNumber(input);
            System.out.println(result);
        }
    }

    /**
     * 解题思路：逆序遍历
     */
    public static String largestOddNumber(String num) {
        char[] arr = num.toCharArray();
        for (int i = arr.length - 1; i >= 0; i--) {
            if ((arr[i] - '0') % 2 == 1) {
                // 当前数是奇数时，[0, i + 1] 的数字即是最大的奇数字符串
                return num.substring(0, i + 1);
            }
        }
        return "";
    }
}
